package com.codedancer.campus.manager.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codedancer.campus.common.exception.BusinessException;
import com.codedancer.campus.manager.service.IFeedbackRecordService;
import com.codedancer.campus.manager.service.bo.feedbackRecord.FeedbackRecordPageSearchBo;
import com.codedancer.campus.manager.service.vo.feedbackRecord.FeedbackRecordPageVo;
import com.codedancer.campus.mapper.FeedbackRecordMapper;
import com.codedancer.campus.po.FeedbackRecord;
import com.codedancer.campus.qo.feedbackRecord.FeedbackRecordPageSearchQo;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class FeedbackRecordServiceImpl implements IFeedbackRecordService {

    @Autowired
    private FeedbackRecordMapper feedbackRecordMapper;

    /**
     * 分页查询反馈记录
     *
     * @param feedbackRecordPageSearchBo
     * @return
     */
    @Override
    public IPage<FeedbackRecordPageVo> page(FeedbackRecordPageSearchBo feedbackRecordPageSearchBo) {
        FeedbackRecordPageSearchQo feedbackRecordPageSearchQo = BeanUtil.copyProperties(
            feedbackRecordPageSearchBo, FeedbackRecordPageSearchQo.class);
        IPage<FeedbackRecord> feedbackRecordIPage = feedbackRecordMapper.page(
            new Page<FeedbackRecord>(feedbackRecordPageSearchBo.getPage(),
                feedbackRecordPageSearchBo.getSize()), feedbackRecordPageSearchQo);
        IPage<FeedbackRecordPageVo> feedbackRecordPageVoIPage = new Page<>();
        List<FeedbackRecordPageVo> feedbackRecordPageVos;
        feedbackRecordPageVos = feedbackRecordIPage.getRecords().stream().map(feedbackRecord -> {
            FeedbackRecordPageVo feedbackRecordPageVo = BeanUtil.copyProperties(feedbackRecord,
                FeedbackRecordPageVo.class);
            feedbackRecordPageVo.setFeedbackUserName(
                feedbackRecord.getFeedbackUser().getNickName());
            feedbackRecordPageVo.setFeedbackRecordTypeName(
                feedbackRecord.getFeedbackRecordType().getName());
            return feedbackRecordPageVo;
        }).collect(Collectors.toList());
        feedbackRecordPageVoIPage.setRecords(feedbackRecordPageVos);
        feedbackRecordPageVoIPage.setTotal(feedbackRecordIPage.getTotal());
        return feedbackRecordPageVoIPage;
    }

    /**
     * 删除反馈记录
     *
     * @param uuid
     */
    @Override
    public void delete(String uuid) {
        if (!feedbackRecordMapper.existsByUuId(uuid)) {
            throw new BusinessException("反馈记录不存在");
        }
        feedbackRecordMapper.deleteById(uuid);
    }
}
